<?php
// -----------------------------------------------------------------------------
// include
// -----------------------------------------------------------------------------

include "../config/config.php";
include "../auth.php";
include "../DBManager.php";
include "../vendor/autoload.php";

// -----------------------------------------------------------------------------
// 农历库相关
// -----------------------------------------------------------------------------

use Overtrue\ChineseCalendar\Calendar;
$calendar = new Calendar();

// -----------------------------------------------------------------------------
// 访问设置
// -----------------------------------------------------------------------------

if (!(isset($_GET["keyword"]) || isset($_GET["tableName"]))) {
    exit("需要传入有效的参数！");
}

$keyword = $_GET["keyword"];
$tableName = $_GET["tableName"];

// -----------------------------------------------------------------------------
// 时间日期相关设置
// -----------------------------------------------------------------------------

/* 设置时区 */

date_default_timezone_set($region);

/* 取得 the 日期 of 今日 */

$todayObj = new DateTime("today");
$todayStr = $todayObj->format("Ymd");

// -----------------------------------------------------------------------------
// 数据库相关设置
// -----------------------------------------------------------------------------

$dbPath = "../" . $dbPath; // 不加这行语句的话会报错，提示找不到对应路径
$db = new DBManager("sqlite:" . $dbPath);

// -----------------------------------------------------------------------------

echo <<<BBB
<!--list group start-->
<div class="list-group mt-3 mb-3">
BBB;

// -----------------------------------------------------------------------------

$rows = $db->search($keyword, $tableName);
if ($rows == false) {
    echo "暂时没有相关结果。";
} else {
    foreach ($rows as $row) {

        $title = "";
        $id = "";
        $url = "";

        switch ($tableName) {
            case "specificDate":
                $title = $row["date"];
                $id = $row["date"];
                $url = "../viewByDate.php?date=" . $id;
                break;
            case "finished":
                $title = $row["date"];
                $id = $row["date"];
                $url = "../viewByDate.php?date=" . $id;
                break;
            case "yearlyLunar":

                $id = $row["monthAndDay"];

                $isLeap = "";
                if (strlen($id) == 5) {
                    $isLeap = "闰";
                }

                $month = substr($id, -4, 2);        // 形如：02
                $monthChinese = $calendar->toChinaMonth($month); // 形如：二月

                $day = substr($id, -2, 2);    // 形如：03
                $dayChinese = $calendar->toChinaDay($day); // 形如：初三

                $monthAndDay = $isLeap . $monthChinese . $dayChinese; // 形如：闰二月初三

                $title = "每年 - 农历 - " . $monthAndDay;

                $url = "../edit.php?from=yearlyLunar&monthAndDay=" . $id;
                break;

            case "yearly":
                $id = $row["monthAndDay"];
                $month = substr($id, -4, 2); // 形如：02
                $day = substr($id, -2, 2); // 形如：03
                $title = "每年 - 阳历 - " . $month . "月" . $day . "号";
                $url = "../edit.php?from=yearly&monthAndDay=" . $id;
                break;
            case "monthly":
                $id = $row["dayOfMonth"];
                if ($id < 10) {
                    $id = "0" . $id;
                }
                $title = "每月 - 阳历 - " . $id . "号";
                $url = "../edit.php?from=monthly&dayOfMonth=" . $id;
                break;
            case "monthlyLunar":
                $id = $row["dayOfMonth"];
                $dayOfMonthLunar = $calendar->toChinaDay($id);
                $title = "每月 - 农历 - {$dayOfMonthLunar}";
                $url = "../edit.php?from=monthlyLunar&dayOfMonth=" . $id;
                break;
            case "weekly":
                $id = $row["dayOfWeek"];
                $days = ["日", "一", "二", "三", "四", "五", "六"];
                $xinqi = $days[$id];
                $title = "每周". $xinqi;
                $url = "../edit.php?from=weekly&dayOfWeek=" . $id;
                break;
            case "daily":
                $title = "每日";
                $url = "../edit.php?from=daily";
                break;
            case "storageBox":
                $title = $row["title"];
                $id = $row["id"];
                $url = "../storageBox/view.php?id=" . $id;
                break;
            case "goals":
                $title = $row["description"];
                $id = $row["id"];
                $url = "../goalsManager/view.php?id=" . $id;
                break;
            case "summariesAndPlans":
                $title = $row["title"];
                $id = $row["id"];
                $url = "../summariesAndPlans/view.php?id=" . $id;
                break;
            case "shoppingList":
                $title = $row["title"];
                $id = $row["id"];
                $url = "../shoppingList/view.php?id=" . $id;
                break;
        }

        if ($id == $todayStr) {
            echo <<<BBB
                <a href="$url" class="list-group-item list-group-item-action list-group-item-info" target="_self">$title (今天)</a>
            BBB;
        } else {
            echo <<<BBB
                <a href="$url" class="list-group-item list-group-item-action" target="_self">$title</a>
            BBB;
        }

    }
}

// -----------------------------------------------------------------------------

echo <<<BBB
</div>
<!--list group end-->
BBB;

// -----------------------------------------------------------------------------
